/*
 * RouteLinkAlight.java
 *
 * Created on March 5, 2007, 3:27 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.core.routenet;

import org.fpdev.core.transit.SubRoute;
import org.fpdev.core.transit.Timetable;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.Path;
import org.fpdev.core.basenet.BNode;
import org.fpdev.core.transit.*;
import java.awt.Color;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.fpdev.core.FPEngine;
import org.fpdev.core.basenet.*;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;

/**
 *
 * @author demory
 */
public class RLinkAlight extends RLink {

  private RLinkSub rLink_;
  private int dli_;
  private BNode endSNode_; // street node that is the dest. of the walk portion
  private BNode walkStartNode_;
  private List<BLink> accessLinks_;
  private int accessLenFt_;

  /** Creates a new instance of CRouteLinkAlight */
  public RLinkAlight(BNode endSNode, RLinkSub rLink, int dli, List<BLink> accessLinks, FPEngine engine) {
    rLink_ = rLink;
    dli_ = dli;
    endSNode_ = endSNode;
    accessLinks_ = new LinkedList<BLink>(); // accessLinks;
    Iterator<BLink> links = accessLinks.iterator();
    accessLenFt_ = 0;
    Path walkPath = new Path(engine);

    while (links.hasNext()) {
      BLink link = links.next();
      accessLenFt_ += link.getLengthFeet();
      //if(!link.isStation()) {
      walkPath.addToEnd(link);
      accessLinks_.add(link);
    //}
    }
    if (walkPath.linkCount() > 0) {
      walkStartNode_ = (walkPath.linkCount() == 1) ? engine.getBaseNet().opposite(endSNode_, walkPath.startLink()) : walkPath.startNode();
    }
  //System.out.println(" - accLenFt: "+accessLenFt_);
  }

  public RNode getStartRouteNode() {
    return rLink_.getFromNode();
  }

  public Iterator<BLink> getAccessLinks() {
    return accessLinks_.iterator();
  }

  public int getAccessLenFt() {
    return accessLenFt_;
  }

  public int accessLinkCount() {
    return accessLinks_.size();
  }

  public int getType() {
    return RLINK_ALIGHT;
  }

  public SubRoute getSubRoute() {
    return rLink_.getSubRoute();
  }

  public String getID() {
    return "al_" + rLink_.getID() + "_" + dli_;
  }

  public int getStartDLI() {
    return rLink_.getStartDLI();
  }

  public int getEndDLI() {
    return dli_;
  }

  public BNode getWalkStart() { //Graph sGraph) {
    return walkStartNode_;
  /*
  // work backwards from end node
  CNode node = endSNode_;
  //Position p = accessLinks_. last();
  //for(int i=0; i<accessLinks_.size(); i++) {
  ListIterator<CLink> links = accessLinks_.listIterator(accessLinks_.size()); 
  while(links.hasPrevious()) {
  CLink link = links.previous();
  System.out.println("snode "+node.getID()+" link "+link.getID());
  node = (CNode) sGraph.opposite(node.getVertex(), link.getEdge()).element();
  //if(i+1 < accessLinks_.size()) p = accessLinks_.before(p);
  }
  return node;*/
  }

  public int timeCostFW(int inTime, CalendarService service, double walkSpeedFPS, int transferTime) {
    if (!rLink_.getSubRoute().hasTable(service)) {
      return 100000000;
    }
    //if(rLink_.getSubRoute().getRoute().getID().equals("9"))
    //System.out.println("tcFW/al "+getID()+" @ "+CUtil.sTimeToStr(inTime));
    double secs = (double) accessLenFt_ / walkSpeedFPS;
    int run = rLink_.getSubRoute().getNextRun(service, inTime + transferTime, rLink_.getStartDLI());
    if (run == 0) {
      return 100000000;
    }
    int outTime = rLink_.getSubRoute().getTime(service, run, dli_ + 1);
    lastRun_ = run;
    return (outTime - inTime) + (int) secs;

  }

  public int timeCostBW(int outTime, CalendarService service, double walkSpeedFPS, int transferTime) {
    if (!rLink_.getSubRoute().hasTable(service)) {
      return 100000000;
    }
    double secs = (double) accessLenFt_ / walkSpeedFPS;
    int depTime = outTime - (int) secs;
    int run = rLink_.getSubRoute().getPrevRun(service, depTime - transferTime, dli_);
    if (run == 0) {
      return 100000000;
    }
    int inTime = rLink_.getSubRoute().getTime(service, run, rLink_.getStartDLI());
    lastRun_ = run;
    return (outTime - inTime);
  }

  public void draw(MapCanvas canvas) {
    canvas.setColor(new Color(0, 128, 0));
    canvas.drawLine(rLink_.getToNode().getX(), rLink_.getToNode().getY(), endSNode_.getX(), endSNode_.getY(), true);

  }
}
